package org.budo.mongo.jdbc.driver.statement.impl;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Map;

import org.budo.jdbc.driver.AbstractBudoJdbcPreparedStatement;
import org.budo.jdbc.driver.resultset.impl.ObjectArrayResultSet;
import org.budo.mongo.jdbc.driver.executor.MongoStatementExecutor;
import org.budo.mongo.jdbc.driver.statement.AbstractMongoStatement;
import org.budo.mongo.jdbc.driver.statement.StatementDocument;

/**
 * @author li
 */
public class MongoCountStatement extends AbstractMongoStatement {
    public void execute(PreparedStatement preparedStatement) throws SQLException {
        StatementDocument statementDocument = this.statementDocument(preparedStatement);
        Map<String, Object> whereDocument = statementDocument.getWhere();

        MongoStatementExecutor statementExecutor = this.statementExecutor(preparedStatement);
        Long count = statementExecutor.count(this.getCollectionName(), whereDocument);

        ObjectArrayResultSet<Object> resultSet = new ObjectArrayResultSet<Object>(this.getResultSetMetaData(), new Object[] { count });
        ((AbstractBudoJdbcPreparedStatement) preparedStatement).setResultSet(resultSet);
    }
}